package xyf.p01.array_nodelist;

import commons.ListNode;

/**
 * ClassName: J025_Merge
 * Description:
 * Author: xyf
 * Date: 9/7/22 7:40 PM
 * Version: 1.0
 **/
public class J025_Merge
{
    public ListNode mergeTwoLists(ListNode l1, ListNode l2)
    {
        ListNode dummyHead = new ListNode();
        ListNode cur = dummyHead;
        while (l1 != null || l2 != null)
        {
            if (l1 == null)
            {
                cur.next = l2;
                break;
            }
            if (l2 == null)
            {
                cur.next = l1;
                break;
            }

            if (l1.val <= l2.val)
            {
                cur.next = new ListNode(l1.val);
                l1 = l1.next;
            }
            else
            {
                cur.next = new ListNode(l2.val);
                l2 = l2.next;
            }
            cur = cur.next;
        }
        return dummyHead.next;
    }
}
